package com.antgroup.seckill.business.utils;

import com.antgroup.seckill.business.common.enums.RespBeanEnum;
import com.antgroup.seckill.business.common.vo.response.RespBean;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

import static com.antgroup.seckill.business.common.constants.CommonConstant.SALT;

/**
 * @author wangxy
 * @date 2024/7/24 23:35
 */
public class MD5Util {

    private static final AtomicInteger sequence = new AtomicInteger(0);
    private static final int TIMESTAMP_LENGTH = 12; // 时间戳长度
    private static final int HASH_LENGTH = 4; // 表名哈希值长度
    private static final int SEQUENCE_LENGTH = 2; // 序列号长度


    public static String getMd5(String src) {
        return DigestUtils.md2Hex(src);
    }

    /**
     * 第一次加密
     *
     * @param inputPass
     * @return
     */
    public static String inputPassToFromPass(String inputPass) {
        String str = "" + SALT.charAt(0) + SALT.charAt(2) + inputPass + SALT.charAt(5) + SALT.charAt(4);
        return getMd5(str);
    }


    /**
     * 第二次加密，最终存到数据库的密码
     *
     * @param fromPass
     * @param salt
     * @return
     */
    public static String fromPassToDBPass(String fromPass, String salt) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + fromPass + salt.charAt(5) + salt.charAt(4);
        return getMd5(str);
    }




    /**
     * 生成数据库唯一ID
     *
     * @param tableName
     * @return
     */
    private static String generateUniqueID(String tableName) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String dateTime = LocalDateTime.now().format(formatter);

        // 对表名进行哈希计算
        String tableHash = getHash(tableName);

        // 生成唯一ID：时间 + 表名哈希的前部分
        String uniqueID = dateTime + tableHash.substring(0, Math.min(18 - dateTime.length(), tableHash.length()));

        return uniqueID;
    }

    private static String getHash(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = digest.digest(input.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-256 algorithm not found", e);
        }
    }


    public static void main(String[] args) {
        //前端密码：8694dd505f2f2ee9f32a7262eec95b1d
        System.out.println(inputPassToFromPass("123456"));

        //存往数据库密码
        System.out.println(fromPassToDBPass("8694dd505f2f2ee9f32a7262eec95b1d", "1a2b3c4d"));

        System.out.println(generateUniqueID("os_user"));

        RespBean error = RespBean.error(RespBeanEnum.LOGIN_ERROR);
        System.out.println(error);

    }

}
